<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\CommandService;
use App\Models\DeviceInfo;
use App\Models\HostInfo;

use Log;

class ApiCommandController extends BaseController
{
    private $_model;
    private $_hostInfo;
    private $_service;

    public function __construct(){
        $this->_service = new CommandService();
        $this->_model = new DeviceInfo();
        $this->_hostInfo = new HostInfo();
    }

    /** 
     *  单路开关
     */
    public function sentCmd(Request $r) {
        $id = $r->id;
        $flag = $r->flag;
        $cmd =  $r->cmd;
        // $msg = $this->_service->sendCmd($flag.'_'.$cmd);
        $msg = $this->_service->sendCmd($flag, $cmd);
        // $msg = 'a';
        if(!empty($msg)) {
            $this->_model->editArray(['id'=>$id, 'enable'=>1], ['status' =>$flag]);
        }
        $this->send(200, $msg);
    }

    // 全部控制
    public function controlAll(Request $r) {
        $flag = $r->flag;
        $msg = $this->_service->controlAll($flag);
        // $msg = '';
        if(!empty($msg)) {
            $this->_model->editArray(['enable'=>1], ['status' =>$flag]);
            $this->_model->editHostArray(['enable'=>1], ['status'=>0]);
        }
        $this->send(200, $msg);
    }
    // 区域电路
    public function getItemDevices(Request $r) {
        $item_id = $r->item_id;
        $_info = $this->_model->getall(['enable'=>1, 'area'=>$item_id], ['id', 'name', 'code', 'cmd']);
        $this->send(200, '成功', $_info);
    }

    // 全部电路
    public function getDevices(Request $r) {
        $_info = $this->_model->getall(['enable'=>1], ['id', 'name', 'code', 'cmd', 'status', 'type']);
        $this->send(200, '成功', $_info);
    }

    // 全部主机状态
    public function updateHost(Request $r) {
        $this->_model->editHostArray(['enable'=>1], ['status'=>1]);
        $this->send(200, '成功');
    }

    // 全部主机
    public function getHosts(Request $r) {
        $item_id = $r->item_id;
        $_info = $this->_hostInfo->getall(['enable'=>1], ['id', 'name', 'desc', 'ip', 'cmd', 'sys', 'status']);
        $this->send(200, '成功', $_info);
    }
    // // 关机
    // public function hostShutdown(Request $r) {
    //     $id = $r->id;
    //     $sys = $r->sys;
    //     $ip = $r->ip;
    //     $cmd =  $r->cmd;
    //     // $_info = $this->_model->getall(['enable'=>1], ['id', 'name', 'code', 'cmd']);
    //     // $msg = $this->_service->shutdownHost($sys, $ip, $cmd);

    //     $_cmd = '';
    //     if($sys == 'win') {
    //         $_cmd = sprintf($cmd, $ip);
    //     } else {
    //         $_cmd = sprintf($cmd, $id, $ip);
    //     }
    //     exec($_cmd, $out);
    //     $msg = $_cmd;
    //     log:info($out);
    //     $this->send(200, '成功', $msg);
    // }

    // 关机
    public function hostShutdown(Request $r) {
        $id = $r->id;
        $ip = $r->ip;
        $cmd =  $r->cmd;
        $_cmd = sprintf($cmd, $id, $ip);
        exec($_cmd, $out);
        $_r = '';
        foreach($out as $key=>$value){
            $_r = $_r . iconv("GBK", "UTF-8", $value);
        }
        $this->send(200, '成功', $_r);
    }

    // 查看主机状态
    public function checkHost(Request $r) {
        $id = $r->id;
        $ip = $r->ip;
        log::info($ip);
        exec("ping -n 2 ". $ip, $out);
        $_r = '';
        foreach($out as $key=>$value){
            $_r = $_r . iconv("GBK", "UTF-8", $value);
        }
        log::info($_r);
        if(strpos($_r, '无法访问目标主机') !==false || strpos($_r, '请求超时') !==false) {
            //
            $this->_model->editHostArray(['enable'=>1, 'id'=>$id], ['status'=>0]);
            echo 0;
        } elseif (strpos($_r, '时间') !==false) {
            echo 1;
            // 测试 发布时 要注销
            // $this->_model->editHostArray(['enable'=>1, 'id'=>$id], ['status'=>0]);
            // echo 0;
            // 测试 发布时 要注销
        } else {
            echo 2;
             // 测试 发布时 要注销
            // $this->_model->editHostArray(['enable'=>1, 'id'=>$id], ['status'=>0]);
            // echo 0;
            // 测试 发布时 要注销
        }

        // echo 0;
    }
}